﻿using AllanXingCommonLibrary;
using cnBlogLibrary;
using cnBlogs.Common;
using cnBlogs.Model;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;


namespace cnBlogs.ViewModel
{
    public class MainPageViewModel : GetFeedsBase
    {
        private GeneratorIncrementalLoadingClass<ArticleInfo> _articleInfoList;

        public GeneratorIncrementalLoadingClass<ArticleInfo> ArticleInfoList
        {
            get { return this._articleInfoList; }
            private set { this.SetProperty(ref this._articleInfoList, value); }
        }

        protected List<ArticleInfo> _rangeList = new List<ArticleInfo>();

        protected uint _pageCount;

        private ArticleCategoryViewModel _articleCategoryViewModel;

        private string _categoryUrl;

        public ArticleCategoryViewModel ArticleCategoryViewModel
        {
            get { return _articleCategoryViewModel; }
            set { _articleCategoryViewModel = value; }
        }


        public MainPageViewModel(string categoryUrl = null)
        {
            _categoryUrl = categoryUrl;
            ArticleInfoList = new GeneratorIncrementalLoadingClass<ArticleInfo>(200, IncrementalM);
        }

        protected virtual async Task<List<ArticleInfo>> IncrementalM(uint pageCount)
        {
            _pageCount = pageCount;
            await GetResource();
            return this._rangeList;
        }
        protected override void Parse(string feed)
        {
            //because zhe feed is sames
            if (_articleCategoryViewModel == null)
            {
                _articleCategoryViewModel = new ArticleCategoryViewModel();
                Task task = _articleCategoryViewModel.GetResource(feed);
            }

            _rangeList.Clear();
            MatchCollection matches = Regex.Matches(feed, "<div class=\"post_item\">.*?<div class=\"digg\">.*?<div class=\"post_item_body\".*?></div>");
            ArticleInfoList.HasCount = !(matches.Count < 20);
            foreach (Match matche in matches)
            {
                ArticleInfo artinfo = new ArticleInfo();
                string regex_article = matche.Value;
                GroupCollection gp = Regex.Match(regex_article, "<h3><a class=\"titlelnk\" href=\"(?<link>.*?)\".*?>(?<title>.*?)</a></h3>").Groups;
                artinfo.Title = gp["title"].Value;
                artinfo.Link = gp["link"].Value;
                artinfo.CommentCount = int.Parse(Regex.Match(regex_article, "<span class=\"diggnum\" id=\".*?\">(?<CommentCount>.*?)</span>").Groups["CommentCount"].Value);
                GroupCollection gp1 = Regex.Match(regex_article, "<p class=\"post_item_summary\">(?<Overview>.*?)</p>").Groups;
                artinfo.Overview = gp1["Overview"].Value.Replace("\r", string.Empty).Trim();
                GroupCollection gpwriteraAndtime = Regex.Match(regex_article, "<a href=\".*?\" class=\"lightblue\">(?<writer>.*?)</a> \r (?<PublishTime>.*?)\r *<span class=\"article_comment\">").Groups;
                artinfo.PublishTime = gpwriteraAndtime["PublishTime"].Value.TrimStart();
                artinfo.Writer = gpwriteraAndtime["writer"].Value;
                Match m_hl = Regex.Match(artinfo.Overview, "<a href=\"(?<writerhomelink>.*?)\" target=\"_blank\">");
                if (m_hl.Length > 0)
                {
                    artinfo.Overview = artinfo.Overview.Replace(m_hl.Value, string.Empty).Trim();
                    artinfo.WriterHomeLink = m_hl.Groups["writerhomelink"].Value;
                    Match mil = Regex.Match(artinfo.Overview, "<img .*? src=\"(?<WriterAvatar>.*?)\" .*?/></a>");
                    artinfo.Overview = artinfo.Overview.Replace(mil.Value, string.Empty);
                    artinfo.WriterAvatar = mil.Groups["WriterAvatar"].Value;

                }
                _rangeList.Add(artinfo);
            }
        }


        #region songzheng's demo
        //public static void ParseFeed(string feed, IList<object> items)
        //{
        //    items.Clear();
        //    feed = feed.Replace("\n", string.Empty);
        //    MatchCollection matches = Regex.Matches(feed, "<div class=\"newslist\">.*?</dl>\\s*?</div>");

        //    foreach (Match match in matches)
        //    {
        //        string article = match.Value;

        //        string topic = Regex.Match(article, "<dt class=\"topic\" >.*?</dt>").Value;
        //        string link = Regex.Match(topic, "<a href=\"(?<link>.*?)\"").Groups["link"].Value;
        //        string title = Regex.Match(topic, "<strong>(?<title>.*?)</strong>").Groups["title"].Value;
        //        title = WebUtility.HtmlDecode(title);

        //        string author = Regex.Match(article, "<dd class=\"author\"><span>(?<author>.*?)</span></dd>").Groups["author"].Value;
        //        author = Regex.Match(author, "\\d{4}-\\d{2}.*").Value;
        //        int pos = author.IndexOf(" | ");
        //        string date = author.Substring(0, pos);
        //        string readCounter = author.Substring(pos + 3);

        //        string desc = Regex.Match(article, "<dd class=\"desc\">.*?</dd>").Value;
        //        string categoryLink = Regex.Match(desc, "<a href=\"(?<link>.*?)\"").Groups["link"].Value;
        //        string imageLink = Regex.Match(desc, "<img src=\"(?<link>.*?)\"").Groups["link"].Value;
        //        string categoryName = Regex.Match(desc, "alt=\"(?<category>.*?)\"").Groups["category"].Value;
        //        string summary = Regex.Match(desc, "<span>.*</span>").Value;
        //        summary = Regex.Replace(summary, "<.*?>", string.Empty);
        //        summary = WebUtility.HtmlDecode(summary).Trim();

        //        ArticleItem articleItem = new ArticleItem
        //        {
        //            Title = title,
        //            PubDate = date,
        //            ReadCounter = readCounter,
        //            ArticleLink = link,
        //            CategoryLink = categoryLink,
        //            CategoryName = categoryName,
        //            ImageLink = imageLink,
        //            Summary = summary,
        //            Background = GetBrush(categoryLink),
        //        };

        //        items.Add(articleItem);
        //    }
        //} 
        #endregion

        protected override string GetUrl()
        {
            string url = AppResource.cnBlogsUlr;
            if (_categoryUrl == null)//mianpage
                url += ("p" + _pageCount);
            else//category page
                url += (_categoryUrl + _pageCount);
            return url;
        }
    }
}
